package com.ophiux.utils.image;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

import com.github.binarywang.utils.qrcode.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

/**
 * 二维码生成工具（来自网络）
 */
public class QRCodeGenerator {
	private static String strNetImageToBase64;

	/**
	 * 网络图片转换Base64的方法
	 *
	 * @param netImagePath     
	 */
	private static void NetImageToBase64(String netImagePath) {
		final ByteArrayOutputStream data = new ByteArrayOutputStream();
		try {
			// 创建URL
			URL url = new URL(netImagePath);
			final byte[] by = new byte[1024];
			// 创建链接
			final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5000);

			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						InputStream is = conn.getInputStream();
						// 将内容读取内存中
						int len = -1;
						while ((len = is.read(by)) != -1) {
							data.write(by, 0, len);
						}
						// 对字节数组Base64编码
						BASE64Encoder encoder = new BASE64Encoder();
						strNetImageToBase64 = encoder.encode(data.toByteArray());
						System.out.println("网络图片转换Base64:data:image/jpg;base64," + strNetImageToBase64);
						// 关闭流
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}).start();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 本地图片转换Base64的方法
	 *
	 * @param imgPath     
	 */

	private static void ImageToBase64(String imgPath) {
		byte[] data = null;
		// 读取图片字节数组
		try {
			InputStream in = new FileInputStream(imgPath);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		// 返回Base64编码过的字节数组字符串
		System.out.println("本地图片转换Base64:" + encoder.encode(Objects.requireNonNull(data)));
	}

	/**
	 * 生成二维码，返回字节流
	 *
	 * @param text   二维码需要包含的信息
	 * @param width  二维码宽度
	 * @param height 二维码高度
	 * @return
	 * @throws WriterException
	 * @throws IOException
	 */
	public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
		QRCodeWriter qrCodeWriter = new QRCodeWriter();
		BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);

		ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
		MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
		byte[] pngData = pngOutputStream.toByteArray();
		return pngData;
	}

	public static void main(String[] args) throws WriterException, IOException {
		// 第一个:把网络图片装换成Base64
		String netImagePath = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQG-7jwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyTVhoeXhGa1FjU20xamhRU3h5Y3AAAgTdMnZiAwT0AQAA";
		// 下面是网络图片转换Base64的方法
		NetImageToBase64(netImagePath);

		// 下面是本地图片转换Base64的方法
		String imagePath = "本地图片路径";
		ImageToBase64(imagePath);

		byte[] data = getQRCodeImage("http://188.188.188.35:8083/#/?id=5", 300, 300);
		// 对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		// 返回Base64编码过的字节数组字符串
		System.out.println("本地图片转换Base64:data:image/jpg;base64," + encoder.encode(data));
	}

}